package LinkedList;

import java.util.Iterator;

public class SingLinkedList implements ListFunction, Iterable<Integer>{

    static class Node {
        public int value;
        public Node next;

        public Node() {
        }

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    private Node hand;
    private int size;

    public SingLinkedList () {
    }


    @Override
    public void addFirst(int value) {
        if (hand == null) {
            hand = new Node(value,null);
            System.out.println("头插成功!!!");
            size++;
            return;
        }

/*        Node temp = new Node(value,hand);
        hand = temp;
        temp = null;*/
        hand = new Node(value,hand);
        System.out.println("头插成功!!!");
        size++;

    }

    @Override
    public void addLast(int value) {
        if (hand == null) {
            hand = new Node(value,null);
            System.out.println("尾插成功!!!");
            size++;
            return;
        }

        Node temp = hand;
        while (temp.next != null) {
            temp = temp.next;
        }
        temp.next = new Node(value,null);
        System.out.println("尾插成功!!!");
        size++;
    }

    @Override
    public void add(int index, int value) {
        if (index < 0 || index > size) {
            throw new RuntimeException("输入非法!!!");
        }

        if (hand == null) {
            hand = new Node(value,null);
            size++;
            System.out.println("成功插入!!!");
            return;
        }

        //考虑头插
        if (index == 0) {
            addFirst(value);
            size++;
            System.out.println("成功插入!!!");
            return;
        }

        //考虑尾插
        if (index == size) {
            addLast(value);
            size++;
            System.out.println("成功插入!!!");
            return;

        }

        //既不是头，也不是尾
        Node temp = hand;
        for (int i = 1; i <= index - 1 ; i++) {
            temp = temp.next;
        }
        Node next = temp.next;
        temp.next = new Node(value,next);
        size++;
        System.out.println("成功插入!!!");

    }

    @Override
    public boolean contains(int key) {
        if (hand == null) {
            System.out.println("找不到!!!");
            return false;
        }

        Node p = hand;
        while ( p != null) {
            if (p.value == key) {
                System.out.println("找到了!!!");
                return true;
            }
            p = p.next;
        }

        System.out.println("找不到!!!");
        return false;
    }

    @Override
    public void remove(int key) {
        if (hand == null) {
            throw new RuntimeException(" hand == null !!!");
        }
        Node slow = hand;
        Node fast = hand.next;

        if (slow.value == key) {
            hand = fast;
            System.out.println("删除成功!!!");
            return;
        }

        while ( fast != null) {
            if (fast.value == key) {
                slow.next = fast.next;
                System.out.println("删除成功!!!");
                size--;
                return;
            } else {
                fast = fast.next;
                slow = slow.next;
            }
        }
        System.out.println("该链表中没有该值的节点，删除失败!!!");

    }

    @Override
    public void removeAllKey(int key) {
        if (hand == null) {
            throw new RuntimeException(" hand == null !!!");
        }
        Node slow = hand;
        Node fast = hand.next;

        while ( fast != null) {
            if (fast.value == key) {
                slow.next = fast.next;
                System.out.println("删除成功!!!");
                size--;
                fast = slow.next;
            }else {
                fast = fast.next;
                slow = slow.next;
            }
        }

        if (slow.value == key) {
            hand = hand.next;
            System.out.println("删除成功!!!");
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void display() {
        Node p = hand;
        while (p != null) {
            System.out.print(p.value + " ");
            p = p.next;
        }
        System.out.println();
    }

    //也可以重写迭代器
    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            Node p = hand;
            @Override
            public boolean hasNext() {
                return p != null;
            }

            @Override
            public Integer next() {
                int key = p.value;
                p = p.next;
                return key;
            }
        };
    }

    @Override
    public void clear() {
        if (hand == null) {
            System.out.println("已经为 null !");
            return;
        }

        Node p = hand;
        while (p != null) {
            Node next = p.next;
            p.next = null;
            p = next;
        }
        hand = null;
        System.out.println("链表清空完成!!!");
    }

}
